{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Predict_Future_Sale.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "metadata": {
        "id": "tSfBT3L1itZr",
        "colab_type": "code",
        "outputId": "bf4eeda7-c72f-49ba-c862-cc83c0095d01",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "cell_type": "code",
      "source": [
        "!ls"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "sample_data\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "0mTKWCsxXo4r",
        "colab_type": "code",
        "outputId": "049a0077-fc85-4db2-81a3-c766a9ea98c8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 459
        }
      },
      "cell_type": "code",
      "source": [
        "!apt-get install -y -qq software-properties-common python-software-properties module-init-tools\n",
        "!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null\n",
        "!apt-get update -qq 2>&1 > /dev/null\n",
        "!curl --header \"Host: launchpadlibrarian.net\" --header \"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36\" --header \"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\" --header \"Accept-Language: zh,zh-CN;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6\" \"https://launchpadlibrarian.net/386846978/google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb\" -o \"google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb\" -L\n",
        "!apt-get -y install -qq fuse\n",
        "!dpkg -i google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb\n",
        "\n",
        "from googleapiclient.discovery import build\n",
        "import io, os\n",
        "from googleapiclient.http import MediaIoBaseDownload\n",
        "from google.colab import auth\n",
        "\n",
        "auth.authenticate_user()\n",
        "\n",
        "drive_service = build('drive', 'v3')\n",
        "results = drive_service.files().list(\n",
        "        q=\"name = 'kaggle.json'\", fields=\"files(id)\").execute()\n",
        "kaggle_api_key = results.get('files', [])\n",
        "\n",
        "filename = \"/content/.kaggle/kaggle.json\"\n",
        "os.makedirs(os.path.dirname(filename), exist_ok=True)\n",
        "\n",
        "request = drive_service.files().get_media(fileId=kaggle_api_key[0]['id'])\n",
        "fh = io.FileIO(filename, 'wb')\n",
        "downloader = MediaIoBaseDownload(fh, request)\n",
        "done = False\n",
        "while done is False:\n",
        "    status, done = downloader.next_chunk()\n",
        "    print(\"Download %d%%.\" % int(status.progress() * 100))\n",
        "os.chmod(filename, 600)\n",
        "!mkdir ~/.kaggle\n",
        "!cp /content/.kaggle/kaggle.json ~/.kaggle/kaggle.json\n",
        "\n",
        "from oauth2client.client import GoogleCredentials\n",
        "creds = GoogleCredentials.get_application_default()\n",
        "import getpass\n",
        "!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL\n",
        "vcode = getpass.getpass()\n",
        "!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "E: Package 'python-software-properties' has no installation candidate\n",
            "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
            "                                 Dload  Upload   Total   Spent    Left  Speed\n",
            "100 1203k  100 1203k    0     0  6204k      0 --:--:-- --:--:-- --:--:-- 6172k\n",
            "Selecting previously unselected package libfuse2:amd64.\n",
            "(Reading database ... 22280 files and directories currently installed.)\n",
            "Preparing to unpack .../libfuse2_2.9.7-1ubuntu1_amd64.deb ...\n",
            "Unpacking libfuse2:amd64 (2.9.7-1ubuntu1) ...\n",
            "Selecting previously unselected package fuse.\n",
            "Preparing to unpack .../fuse_2.9.7-1ubuntu1_amd64.deb ...\n",
            "Unpacking fuse (2.9.7-1ubuntu1) ...\n",
            "Setting up libfuse2:amd64 (2.9.7-1ubuntu1) ...\n",
            "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n",
            "Setting up fuse (2.9.7-1ubuntu1) ...\n",
            "Selecting previously unselected package google-drive-ocamlfuse.\n",
            "(Reading database ... 22306 files and directories currently installed.)\n",
            "Preparing to unpack google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb ...\n",
            "Unpacking google-drive-ocamlfuse (0.7.0-0ubuntu1) ...\n",
            "Setting up google-drive-ocamlfuse (0.7.0-0ubuntu1) ...\n",
            "Download 100%.\n",
            "Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force\n",
            "··········\n",
            "Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force\n",
            "Please enter the verification code: Access token retrieved correctly.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "K102BSsYDGMy",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!mkdir -p drive\n",
        "!google-drive-ocamlfuse -o nonempty drive\n",
        "!ls drive > /dev/null"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "teccmM5G-YOk",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### Setup"
      ]
    },
    {
      "metadata": {
        "id": "3iFXoj-NaTc9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 122
        },
        "outputId": "8949669d-3a45-452b-a5c5-d91db954c34e"
      },
      "cell_type": "code",
      "source": [
        "!mkdir src -p && cd src && git clone https://github.com/fastai/fastai.git"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'fastai'...\n",
            "remote: Enumerating objects: 13467, done.\u001b[K\n",
            "remote: Total 13467 (delta 0), reused 0 (delta 0), pack-reused 13467\u001b[K\n",
            "Receiving objects: 100% (13467/13467), 248.80 MiB | 26.51 MiB/s, done.\n",
            "Resolving deltas: 100% (8770/8770), done.\n",
            "Checking out files: 100% (681/681), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "bg2utfE8aW4R",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "outputId": "153228d4-6d69-47e3-9d73-a2989dc64435"
      },
      "cell_type": "code",
      "source": [
        "!pip3 install -q bcolz graphviz sklearn_pandas isoweek pandas_summary ipywidgets torch torchvision torchtext"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tcmalloc: large alloc 1073750016 bytes == 0x59754000 @  0x7f35dc5102a4 0x594e17 0x626104 0x51190a 0x4f5277 0x510c78 0x5119bd 0x4f5277 0x4f3338 0x510fb0 0x5119bd 0x4f5277 0x4f3338 0x510fb0 0x5119bd 0x4f5277 0x4f3338 0x510fb0 0x5119bd 0x4f6070 0x510c78 0x5119bd 0x4f5277 0x4f3338 0x510fb0 0x5119bd 0x4f6070 0x4f3338 0x510fb0 0x5119bd 0x4f6070\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "D6T70YIYyuKp",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!git config --global user.email 'nikshuang@163.com'\n",
        "!git config --global user.name 'Alex Huang'\n",
        "!pip install -q kaggle"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "fU2PZhxv6iFX",
        "colab_type": "code",
        "outputId": "5ea5243e-5044-4f19-9173-5898b63f3a17",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        }
      },
      "cell_type": "code",
      "source": [
        "import sys\n",
        "sys.path.append(\"/content/src/fastai/old\") # on windows use \\'s instead\n",
        "\n",
        "from fastai.structured import *\n",
        "from fastai.column_data import *\n",
        "from itertools import product\n",
        "np.set_printoptions(threshold=50, edgeitems=20)\n",
        "import gc\n",
        "print(torch.__version__)\n",
        "torch.backends.cudnn.benchmark=True\n",
        "torch.cuda.is_available(), torch.backends.cudnn.enabled"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.4.1\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(True, True)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "metadata": {
        "id": "LNEFSZEW6iAD",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "PATH = Path('data/predict_feature_sales')\n",
        "DISK_PATH = Path('drive/predict_feature_sales')\n",
        "os.makedirs(PATH, exist_ok=True)\n",
        "os.makedirs(DISK_PATH, exist_ok=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "HBTQTiRE6h8D",
        "colab_type": "code",
        "outputId": "c3b1e76d-81cd-44a6-dc7d-e107ca390709",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 340
        }
      },
      "cell_type": "code",
      "source": [
        "!kaggle competitions download -c competitive-data-science-predict-future-sales -p {PATH}"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading item_categories.csv to data/predict_feature_sales\n",
            "\r  0% 0.00/3.49k [00:00<?, ?B/s]\n",
            "100% 3.49k/3.49k [00:00<00:00, 2.64MB/s]\n",
            "Downloading items.csv to data/predict_feature_sales\n",
            "  0% 0.00/1.50M [00:00<?, ?B/s]\n",
            "100% 1.50M/1.50M [00:00<00:00, 103MB/s]\n",
            "Downloading shops.csv to data/predict_feature_sales\n",
            "  0% 0.00/2.91k [00:00<?, ?B/s]\n",
            "100% 2.91k/2.91k [00:00<00:00, 3.62MB/s]\n",
            "Downloading sample_submission.csv.gz to data/predict_feature_sales\n",
            "  0% 0.00/469k [00:00<?, ?B/s]\n",
            "100% 469k/469k [00:00<00:00, 81.0MB/s]\n",
            "Downloading test.csv.gz to data/predict_feature_sales\n",
            "  0% 0.00/1.01M [00:00<?, ?B/s]\n",
            "100% 1.01M/1.01M [00:00<00:00, 122MB/s]\n",
            "Downloading sales_train.csv.gz to data/predict_feature_sales\n",
            " 70% 9.00M/12.9M [00:00<00:00, 20.1MB/s]\n",
            "100% 12.9M/12.9M [00:00<00:00, 31.2MB/s]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ofaujGmV7san",
        "colab_type": "code",
        "outputId": "7afb5491-c6af-44df-ea1a-84f10d3b52c9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        }
      },
      "cell_type": "code",
      "source": [
        "!cd {PATH} && ls *.gz | xargs -IX gunzip X\n",
        "!ls {PATH}"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "item_categories.csv  sales_train.csv\t    shops.csv\n",
            "items.csv\t     sample_submission.csv  test.csv\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "_Xq5E3RblSiW",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### Data Cleaning"
      ]
    },
    {
      "metadata": {
        "id": "ic6f6q8DlXQW",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "train = pd.read_csv(PATH/'sales_train.csv', low_memory=False)\n",
        "test = pd.read_csv(PATH/'test.csv', low_memory=False).set_index('ID')\n",
        "shops = pd.read_csv(PATH/'shops.csv', low_memory=False)\n",
        "items = pd.read_csv(PATH/'items.csv', low_memory=False)\n",
        "cats = pd.read_csv(PATH/'item_categories.csv', low_memory=False)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "0RHZUAJX3tZT",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "aa511fb0-cf7a-4f52-b9ee-5cc3de25cee8"
      },
      "cell_type": "code",
      "source": [
        "train.head(3)"
      ],
      "execution_count": 61,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>date</th>\n",
              "      <th>date_block_num</th>\n",
              "      <th>shop_id</th>\n",
              "      <th>item_id</th>\n",
              "      <th>item_price</th>\n",
              "      <th>item_cnt_day</th>\n",
              "      <th>revenue</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>02.01.2013</td>\n",
              "      <td>0</td>\n",
              "      <td>59</td>\n",
              "      <td>22154</td>\n",
              "      <td>999.0</td>\n",
              "      <td>1.0</td>\n",
              "      <td>999.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>03.01.2013</td>\n",
              "      <td>0</td>\n",
              "      <td>25</td>\n",
              "      <td>2552</td>\n",
              "      <td>899.0</td>\n",
              "      <td>1.0</td>\n",
              "      <td>899.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>05.01.2013</td>\n",
              "      <td>0</td>\n",
              "      <td>25</td>\n",
              "      <td>2552</td>\n",
              "      <td>899.0</td>\n",
              "      <td>-1.0</td>\n",
              "      <td>-899.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "         date  date_block_num  shop_id  item_id  item_price  item_cnt_day  \\\n",
              "0  02.01.2013               0       59    22154       999.0           1.0   \n",
              "1  03.01.2013               0       25     2552       899.0           1.0   \n",
              "2  05.01.2013               0       25     2552       899.0          -1.0   \n",
              "\n",
              "   revenue  \n",
              "0    999.0  \n",
              "1    899.0  \n",
              "2   -899.0  "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 61
        }
      ]
    },
    {
      "metadata": {
        "id": "-bseRcXQ4slw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "54eb9efb-b284-4c9f-f8fc-c8671392b699"
      },
      "cell_type": "code",
      "source": [
        "test.head(2)"
      ],
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>shop_id</th>\n",
              "      <th>item_id</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>ID</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>5</td>\n",
              "      <td>5037</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>5</td>\n",
              "      <td>5320</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "    shop_id  item_id\n",
              "ID                  \n",
              "0         5     5037\n",
              "1         5     5320"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 62
        }
      ]
    },
    {
      "metadata": {
        "id": "O7m8LsYClXIV",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# train.date = pd.to_datetime(train.date, format='%d.%m.%Y')\n",
        "train = train[train.item_cnt_day <= 1000]\n",
        "train = train[train.item_price < 100000]\n",
        "median = train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].item_price.median()\n",
        "train.loc[train.item_price<0, 'item_price'] = median"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "HuP4h0bIlXFE",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Якутск Орджоникидзе, 56\n",
        "train.loc[train.shop_id == 0, 'shop_id'] = 57\n",
        "test.loc[test.shop_id == 0, 'shop_id'] = 57\n",
        "# Якутск ТЦ \"Центральный\"\n",
        "train.loc[train.shop_id == 1, 'shop_id'] = 58\n",
        "test.loc[test.shop_id == 1, 'shop_id'] = 58\n",
        "# Жуковский ул. Чкалова 39м²\n",
        "train.loc[train.shop_id == 10, 'shop_id'] = 11\n",
        "test.loc[test.shop_id == 10, 'shop_id'] = 11"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-BigUgEWC2Hk",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### Feature Engineering"
      ]
    },
    {
      "metadata": {
        "id": "9naBEiNUC6iJ",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def join_df_agg(left, right, aggregation_recipies, col_names=None, col_type=None):\n",
        "  groupby_aggregate_names = []\n",
        "  if col_names is not None:\n",
        "    col_names.reverse()\n",
        "  for i, (groupby_cols, specs) in enumerate(aggregation_recipies):\n",
        "      group_object = right.groupby(groupby_cols)\n",
        "      for select, agg in specs:\n",
        "          if col_names is not None:\n",
        "            groupby_aggregate_name = col_names.pop()\n",
        "          else:\n",
        "            groupby_aggregate_name = '{}_{}_{}'.format('_'.join(groupby_cols), agg, select)\n",
        "          left = left.merge(group_object[select]\n",
        "                                .agg(agg)\n",
        "                                .reset_index()\n",
        "                                .rename(index=str,\n",
        "                                        columns={select: groupby_aggregate_name})\n",
        "                                [groupby_cols + [groupby_aggregate_name]],\n",
        "                                on=groupby_cols,\n",
        "                                how='left')\n",
        "          if col_type: left[groupby_aggregate_name] = left[groupby_aggregate_name].astype(col_type)\n",
        "          groupby_aggregate_names.append(groupby_aggregate_name)\n",
        "  return left, groupby_aggregate_names"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "17ZnqB5JlXBS",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "train['revenue'] = train['item_price'] * train['item_cnt_day']\n",
        "\n",
        "shops.loc[shops.shop_name == 'Сергиев Посад ТЦ \"7Я\"', 'shop_name'] = 'СергиевПосад ТЦ \"7Я\"'\n",
        "shops['city'] = shops['shop_name'].str.split(' ').map(lambda x: x[0])\n",
        "shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'\n",
        "shops['city_code'] = LabelEncoder().fit_transform(shops['city'])\n",
        "shops = shops[['shop_id','city_code']]\n",
        "\n",
        "cats['split'] = cats['item_category_name'].str.split('-')\n",
        "cats['type'] = cats['split'].map(lambda x: x[0].strip())\n",
        "cats['type_code'] = LabelEncoder().fit_transform(cats['type'])\n",
        "# if subtype is nan then type\n",
        "cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x) > 1 else x[0].strip())\n",
        "cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])\n",
        "cats = cats[['item_category_id','type_code', 'subtype_code']]\n",
        "\n",
        "items.drop(['item_name'], axis=1, inplace=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "x-MVYcpKFNmM",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "recipies = [\n",
        "    (['date_block_num', 'shop_id', 'item_id'], [('item_cnt_day', 'sum')]),\n",
        "    (['item_id'], [('item_price', 'mean')]),\n",
        "    (['date_block_num', 'item_id'], [('item_price', 'mean')]),\n",
        "    (['date_block_num','shop_id'], [('revenue', 'sum')]),\n",
        "]\n",
        "col_names = [\n",
        "    'item_cnt_month', 'item_avg_item_price', 'date_item_avg_item_price',\n",
        "    'date_shop_revenue'\n",
        "]\n",
        "\n",
        "matrix, _ = join_df_agg(train, train, recipies, col_names)\n",
        "matrix.item_cnt_month = matrix.item_cnt_month.fillna(0).clip(0, 20)\n",
        "                  \n",
        "recipies = [\n",
        "    (['shop_id'], [('date_shop_revenue', 'mean')]),\n",
        "]\n",
        "col_names = [\n",
        "    'shop_avg_revenue'\n",
        "]\n",
        "matrix, _ = join_df_agg(matrix, matrix, recipies, col_names)\n",
        "matrix['delta_revenue'] = (matrix['date_shop_revenue'] - matrix['shop_avg_revenue']) / matrix['shop_avg_revenue']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "vJsXhjKvFNi3",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "matrix = pd.merge(matrix, shops, on=['shop_id'], how='left')\n",
        "matrix = pd.merge(matrix, items, on=['item_id'], how='left')\n",
        "matrix = pd.merge(matrix, cats, on=['item_category_id'], how='left')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "p6OZO3bM4NDp",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "matrix.drop(['date', 'item_price', 'item_cnt_day', 'revenue'], axis=1, inplace=True)\n",
        "matrix.drop_duplicates(inplace=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "m650__8SFNY4",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "recipies = [\n",
        "    (['date_block_num'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'item_id'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'shop_id'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'item_category_id'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'shop_id', 'item_category_id'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'shop_id', 'type_code'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'shop_id', 'subtype_code'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'city_code'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'item_id', 'city_code'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'type_code'], [('item_cnt_month', 'mean')]),\n",
        "    (['date_block_num', 'subtype_code'], [('item_cnt_month', 'mean')]),\n",
        "]\n",
        "col_names = [\n",
        "    'date_avg_item_cnt', 'date_item_avg_item_cnt', 'date_shop_avg_item_cnt', 'date_cat_avg_item_cnt', \n",
        "    'date_shop_cat_avg_item_cnt', 'date_shop_type_avg_item_cnt', 'date_shop_subtype_avg_item_cnt',\n",
        "    'date_city_avg_item_cnt', 'date_item_city_avg_item_cnt', 'date_type_avg_item_cnt', 'date_subtype_avg_item_cnt'\n",
        "]\n",
        "\n",
        "matrix, _ = join_df_agg(matrix, matrix, recipies, col_names)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "trfTXv4QO5jH",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "matrix['month'] = matrix['date_block_num'] % 12\n",
        "days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])\n",
        "matrix['days'] = matrix['month'].map(days)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "YeoeuhIXRQp0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "6e082a78-e5d8-47b9-eafa-d9fe7577feb6"
      },
      "cell_type": "code",
      "source": [
        "%%time\n",
        "item_shop_cache = {}\n",
        "item_cache = {}\n",
        "matrix['item_shop_last_sale'] = -1\n",
        "matrix['item_last_sale'] = -1\n",
        "\n",
        "for idx, row in matrix.iterrows():    \n",
        "    key = str(row.item_id)+' '+str(row.shop_id)\n",
        "    if key not in item_shop_cache:\n",
        "        if row.item_cnt_month!=0:\n",
        "            item_shop_cache[key] = row.date_block_num\n",
        "    else:\n",
        "        last_date_block_num = item_shop_cache[key]\n",
        "        matrix.at[idx, 'item_shop_last_sale'] = row.date_block_num - last_date_block_num\n",
        "        item_shop_cache[key] = row.date_block_num   \n",
        "   \n",
        "    key = row.item_id\n",
        "    if key not in item_cache:\n",
        "        if row.item_cnt_month!=0:\n",
        "            item_cache[key] = row.date_block_num\n",
        "    else:\n",
        "        last_date_block_num = item_cache[key]\n",
        "        if row.date_block_num>last_date_block_num:\n",
        "            matrix.at[idx, 'item_last_sale'] = row.date_block_num - last_date_block_num\n",
        "            item_cache[key] = row.date_block_num\n",
        "\n",
        "matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')\n",
        "matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "CPU times: user 3min 38s, sys: 365 ms, total: 3min 38s\n",
            "Wall time: 3min 38s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "rSj57DapRQhz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 228
        },
        "outputId": "7e4f8c2a-7cd8-40c6-fe14-5d325631a6ea"
      },
      "cell_type": "code",
      "source": [
        "matrix.columns"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['date_block_num', 'shop_id', 'item_id', 'item_cnt_month',\n",
              "       'item_avg_item_price', 'date_item_avg_item_price', 'date_shop_revenue',\n",
              "       'shop_avg_revenue', 'delta_revenue', 'city_code', 'item_category_id',\n",
              "       'type_code', 'subtype_code', 'date_avg_item_cnt',\n",
              "       'date_item_avg_item_cnt', 'date_shop_avg_item_cnt',\n",
              "       'date_cat_avg_item_cnt', 'date_shop_cat_avg_item_cnt',\n",
              "       'date_shop_type_avg_item_cnt', 'date_shop_subtype_avg_item_cnt',\n",
              "       'date_city_avg_item_cnt', 'date_item_city_avg_item_cnt',\n",
              "       'date_type_avg_item_cnt', 'date_subtype_avg_item_cnt', 'month', 'days',\n",
              "       'item_shop_last_sale', 'item_last_sale', 'item_shop_first_sale',\n",
              "       'item_first_sale'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 29
        }
      ]
    },
    {
      "metadata": {
        "id": "YPbwNL37RQW2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "34764b9e-ed65-4c56-b835-567bf7c70765"
      },
      "cell_type": "code",
      "source": [
        "del items, shops, cats\n",
        "gc.collect()"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "121"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "metadata": {
        "id": "_Td8qcZpO5ff",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "8c78cc07-b5d0-4354-e100-2d664f216f32"
      },
      "cell_type": "code",
      "source": [
        "matrix.reset_index(drop=True, inplace=True)\n",
        "matrix.to_pickle('matrix.pkl')\n",
        "shutil.copy('matrix.pkl', DISK_PATH)"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'drive/predict_feature_sales/matrix.pkl'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    },
    {
      "metadata": {
        "id": "4v9KTtvsX_cB",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### Training"
      ]
    },
    {
      "metadata": {
        "id": "yr9iPe2BZPxJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "972fe277-86a9-41a6-a08d-cf8fe4ad4462"
      },
      "cell_type": "code",
      "source": [
        "shutil.copy(DISK_PATH/'matrix.pkl', '.')"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'./matrix.pkl'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "metadata": {
        "id": "bMvA4KsjeVkv",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "trn_df = pickle.load(open('matrix.pkl', 'rb'))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "HoPO4hX76Fko",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "trn_df = matrix"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "NJNYD30K3tme",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 228
        },
        "outputId": "d0f8c3ee-f9ff-4a27-9bfe-6e8634e71c33"
      },
      "cell_type": "code",
      "source": [
        "trn_df.columns"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['date_block_num', 'shop_id', 'item_id', 'item_cnt_month',\n",
              "       'item_avg_item_price', 'date_item_avg_item_price', 'date_shop_revenue',\n",
              "       'shop_avg_revenue', 'delta_revenue', 'city_code', 'item_category_id',\n",
              "       'type_code', 'subtype_code', 'date_avg_item_cnt',\n",
              "       'date_item_avg_item_cnt', 'date_shop_avg_item_cnt',\n",
              "       'date_cat_avg_item_cnt', 'date_shop_cat_avg_item_cnt',\n",
              "       'date_shop_type_avg_item_cnt', 'date_shop_subtype_avg_item_cnt',\n",
              "       'date_city_avg_item_cnt', 'date_item_city_avg_item_cnt',\n",
              "       'date_type_avg_item_cnt', 'date_subtype_avg_item_cnt', 'month', 'days',\n",
              "       'item_shop_last_sale', 'item_last_sale', 'item_shop_first_sale',\n",
              "       'item_first_sale'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "metadata": {
        "id": "xqki5yrrTFuj",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "cont_cols = list(trn_df.columns)\n",
        "cat_cols = ['date_block_num', 'shop_id', 'item_id', 'city_code',\n",
        "       'item_category_id', 'type_code', 'subtype_code', 'month', 'days']\n",
        "# cat_cols = ['date_block_num', 'shop_id', 'city_code',\n",
        "#        'item_category_id', 'type_code', 'subtype_code', 'month', 'days']\n",
        "y_col = 'item_cnt_month'\n",
        "# skip_cols = ['item_id']\n",
        "skip_cols = []\n",
        "for o in cat_cols + [y_col] + skip_cols:\n",
        "    cont_cols.remove(o)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "WpAHwSKCTFlw",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "for col in cat_cols: trn_df[col] = trn_df[col].astype('category').cat.as_ordered()\n",
        "for col in cont_cols: trn_df[col] = trn_df[col].fillna(0).astype('float32')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "2PWjqWu7FNTF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 175
        },
        "outputId": "0a7c8f40-3764-444a-da0e-64ae2445c262"
      },
      "cell_type": "code",
      "source": [
        "cat_sz = [(o, len(trn_df[o].cat.categories) + 1) for o in cat_cols]\n",
        "cat_sz"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[('date_block_num', 35),\n",
              " ('shop_id', 58),\n",
              " ('item_id', 21807),\n",
              " ('city_code', 32),\n",
              " ('item_category_id', 85),\n",
              " ('type_code', 21),\n",
              " ('subtype_code', 66),\n",
              " ('month', 13),\n",
              " ('days', 4)]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 36
        }
      ]
    },
    {
      "metadata": {
        "id": "BQdbx8HyFNOm",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 175
        },
        "outputId": "98a38c4b-8f8a-4d86-bd8c-637ba7d84325"
      },
      "cell_type": "code",
      "source": [
        "emb_szs = [(o, min(50, o // 2)) for _, o in cat_sz]\n",
        "emb_szs"
      ],
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[(35, 17),\n",
              " (58, 29),\n",
              " (21807, 50),\n",
              " (32, 16),\n",
              " (85, 42),\n",
              " (21, 10),\n",
              " (66, 33),\n",
              " (13, 6),\n",
              " (4, 2)]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 37
        }
      ]
    },
    {
      "metadata": {
        "id": "-IUUhk9EFNMQ",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "df_trn, y, nas, mapper = proc_df(trn_df, 'item_cnt_month', skip_flds=skip_cols, do_scale=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "EPfjUdX_gagY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b4943e8e-1da3-4419-dc38-8a958bfe326f"
      },
      "cell_type": "code",
      "source": [
        "n = len(df_trn); n"
      ],
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1609123"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "metadata": {
        "id": "xxMvF4NZFNGt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 105
        },
        "outputId": "16535ccf-8ba3-4e88-916d-473fb81392db"
      },
      "cell_type": "code",
      "source": [
        "train_ratio = 0.75\n",
        "train_size = int(n * train_ratio)\n",
        "val_idxs = list(range(train_size, n))\n",
        "len(val_idxs), np.array(val_idxs)"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(402281,\n",
              " array([1206842, 1206843, 1206844, 1206845, 1206846, 1206847, 1206848, 1206849, 1206850, 1206851, 1206852,\n",
              "        1206853, 1206854, 1206855, 1206856, 1206857, 1206858, 1206859, 1206860, 1206861, ..., 1609103,\n",
              "        1609104, 1609105, 1609106, 1609107, 1609108, 1609109, 1609110, 1609111, 1609112, 1609113, 1609114,\n",
              "        1609115, 1609116, 1609117, 1609118, 1609119, 1609120, 1609121, 1609122]))"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "metadata": {
        "id": "pGKZghsEFNJI",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "bs = 128\n",
        "md = ColumnarModelData.from_data_frame(PATH, val_idxs, df_trn, y.astype('float32'), cat_cols, bs=bs)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Cc6uQeFdh3Ln",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "emb_drop = 0.04 * 2\n",
        "szs = [1000, 500]\n",
        "ps = np.array([0.25, 0.5])\n",
        "learn = md.get_learner(emb_szs, len(cont_cols), emb_drop, 1, szs=szs, drops=ps, y_range=(0, 20))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "kGADD2oAQBT8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 487
        },
        "outputId": "a272372e-ba9d-4c0f-eb6d-b10b91c37b3e"
      },
      "cell_type": "code",
      "source": [
        "learn"
      ],
      "execution_count": 58,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "MixedInputModel(\n",
              "  (embs): ModuleList(\n",
              "    (0): Embedding(35, 17)\n",
              "    (1): Embedding(58, 29)\n",
              "    (2): Embedding(21807, 50)\n",
              "    (3): Embedding(32, 16)\n",
              "    (4): Embedding(85, 42)\n",
              "    (5): Embedding(21, 10)\n",
              "    (6): Embedding(66, 33)\n",
              "    (7): Embedding(13, 6)\n",
              "    (8): Embedding(4, 2)\n",
              "  )\n",
              "  (lins): ModuleList(\n",
              "    (0): Linear(in_features=225, out_features=1000, bias=True)\n",
              "    (1): Linear(in_features=1000, out_features=500, bias=True)\n",
              "  )\n",
              "  (bns): ModuleList(\n",
              "    (0): BatchNorm1d(1000, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "    (1): BatchNorm1d(500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              "  )\n",
              "  (outp): Linear(in_features=500, out_features=1, bias=True)\n",
              "  (emb_drop): Dropout(p=0.08)\n",
              "  (drops): ModuleList(\n",
              "    (0): Dropout(p=0.25)\n",
              "    (1): Dropout(p=0.5)\n",
              "  )\n",
              "  (bn): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
              ")"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 58
        }
      ]
    },
    {
      "metadata": {
        "id": "5suSxydJJWAy",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 154
        },
        "outputId": "7558ecd8-fe12-409a-86ab-ea13ecfe0198"
      },
      "cell_type": "code",
      "source": [
        "lr = 1e-3\n",
        "learn.fit(lr, 1)"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "01e3a9ae2a9f4bb0816d82e0561b40df",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "HBox(children=(IntProgress(value=0, description='Epoch', max=1, style=ProgressStyle(description_width='initial…"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "  0%|          | 11/9429 [00:00<13:59, 11.21it/s, loss=14.4]"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1006: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\n",
            "  warnings.warn(\"nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\")\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "epoch      trn_loss   val_loss   \n",
            "    0      0.723625   0.677875  \n",
            "\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0.6778747571599526]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 53
        }
      ]
    },
    {
      "metadata": {
        "id": "guAaCFglJWKy",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 188
        },
        "outputId": "2fb20201-9d20-4a1e-f773-531f7e903b17"
      },
      "cell_type": "code",
      "source": [
        "learn.fit(lr / 3, 3)"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "81df1b954ae0441593ed5a40ac66e600",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "HBox(children=(IntProgress(value=0, description='Epoch', max=3, style=ProgressStyle(description_width='initial…"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "  0%|          | 11/9429 [00:00<12:55, 12.15it/s, loss=0.605]"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:1006: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\n",
            "  warnings.warn(\"nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\")\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "epoch      trn_loss   val_loss   \n",
            "    0      0.604653   0.634174  \n",
            "    1      0.621239   0.638807  \n",
            "    2      0.62528    0.631941  \n",
            "\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0.6319414914829438]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 54
        }
      ]
    },
    {
      "metadata": {
        "id": "wZE5z81pPlnQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "143af013-ec44-46ae-bc99-41433e5aa69d"
      },
      "cell_type": "code",
      "source": [
        "learn.crit"
      ],
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<function torch.nn.functional.mse_loss>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 56
        }
      ]
    },
    {
      "metadata": {
        "id": "F-veHtKmPfF2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "6d9a862f-a467-4c38-bce7-764f1a34a21b"
      },
      "cell_type": "code",
      "source": [
        "np.sqrt(0.631941)"
      ],
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.79494716805584"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 55
        }
      ]
    },
    {
      "metadata": {
        "id": "FRxL4IRzPsKt",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}